﻿using FinalTestMachine.Motion;
using FinalTestMachine.Motion.Process;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FinalTestMachine.Connect
{
    class ZebraHelper
    {
        static string filePath; /*= System.IO.Path.Combine(System.Environment.CurrentDirectory, "SysCfg", $"{OtherParamHelper.Cur.ZebraTxtFileName.GetValue}.txt");*/
       
        static string ngfilePath => System.IO.Path.Combine(System.Environment.CurrentDirectory, "SysCfg", $"{OtherParamHelper.Cur.ZebraTxtNGFileName.GetValue}.txt");

        //// 写入文件
        //File.WriteAllText(filePath, content); // 覆盖文件内容
        //Console.WriteLine("内容已写入文件。");

        static ZebraHelper()
        {
            textNameList.Add(OtherParamHelper.Cur.ZebraTxtFileName_Front.GetValue);
            textNameList.Add(OtherParamHelper.Cur.ZebraTxtFileName_Left.GetValue);
            textNameList.Add(OtherParamHelper.Cur.ZebraTxtFileName_Right.GetValue);
            textNameList.Add(OtherParamHelper.Cur.ZebraTxtFileName_DMS.GetValue);
           // textNameList.Add(OtherParamHelper.Cur.ZebraTxtNGFileName.GetValue);
        }


        static void setPath(string msg)
        {
            filePath = System.IO.Path.Combine(System.Environment.CurrentDirectory, "SysCfg", $"{msg}.txt");
        }

        public static List<string> textNameList=new List<string>();

        public static bool SendZebra(string sn, int no,string NG="")
        {
            string msg = GetTXTData(sn,no, NG);
            if (!string.IsNullOrEmpty(msg))
            {
                Process_AutoRun.Cur.WriteToUser("当前给打印机TXT文件数据发送的数据为：" + msg);
                //ConnectFactory.tcpZebraPrint.Send(msg);
                return true;
            }
            else
            {
                return false;
            }
           
        }


        static string GetTXTData(string sn, int no, string NG = "")
        {
            string result = "";
            try
            {
                string nodata="";
                if (sn.Length>7)
                {
                    string presevenSN = sn.Substring(0, 7);

                    if (NG == "NG")
                    {
                        string msg = File.ReadAllText(ngfilePath);
                        Process_AutoRun.Cur.WriteToUser("当前给读取本地文件数据为：" + msg);
                        if (msg.Length > 3)
                        {
                            nodata = msg;
                        }
                        else
                        {
                            Process_AutoRun.Cur.WriteErrToUser("当前读取打印机TXT文件数据错误，长度小于3！");
                            nodata = "";
                        }
                        return nodata;
                    }

                    for (int i = 0; i < textNameList.Count; i++)
                    {
                        if (presevenSN == textNameList[i])
                        {
                            setPath(presevenSN);
                          
                            string onlySN = sn.Substring(7);
                            Process_AutoRun.Cur.WriteToUser("当前给读取本地文件成品料号为：" + presevenSN);
                            string readContent = File.ReadAllText(filePath);
                            Process_AutoRun.Cur.WriteToUser("当前给读取本地文件数据为：" + readContent);
                            if (readContent.Length > 10)
                            {
                                string c = readContent.Replace("NEEDSETSNN", onlySN);

                                string d = c.Replace("YYYY", "硬件版本");

                                string e = d.Replace("XXXXXXX", "空间感知传感器");
                                if (presevenSN == OtherParamHelper.Cur.ZebraTxtFileName_DMS.GetValue)
                                {
                                    result = e.Replace("ZZZZZZZZZ","智能识别摄像头总成") ;
                                }
                                else
                                {
                                    result = e;
                                }
                              
                               
                                return result;
                            }
                            else
                            {
                                Process_AutoRun.Cur.WriteErrToUser("当前读取打印机TXT文件数据错误，长度小于10！");
                            }
                        }
                    }

                }
                else
                {
           
                    Process_AutoRun.Cur.WriteErrToUser("当前SN长度小于7位：" + sn);
                }
              
              //  return result;

            }
            catch (Exception ex)
            {

                 Process_AutoRun.Cur.WriteErrToUser("当前打印机相关处理错误：" + ex.Message);
            }

            return null;

        }
    }
}
